package com.ydj.framework.mysql.generator;

import com.baomidou.mybatisplus.autoconfigure.MybatisPlusPropertiesCustomizer;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

/**
 * @author rick
 */
@Component
@Slf4j
@Primary
public class MysqlIdGenerator implements IdentifierGenerator {

    @Autowired
    private SnowFlakeIdUtils snowFlakeIdUtils;

    @Override
    public Long nextId(Object entity) {
        return snowFlakeIdUtils.generatorId();
    }

    @Bean
    public IdentifierGenerator idGenerator() {
        return new MysqlIdGenerator();
    }

    @Bean
    public MybatisPlusPropertiesCustomizer plusPropertiesCustomizer() {
        return plusProperties -> plusProperties.getGlobalConfig().setIdentifierGenerator(new MysqlIdGenerator());
    }

}
